<template>
  <div class="param-main">
    <el-form ref="tblMain" :model="tblMain" labelPosition="right" inline="" label-suffix=":" label-width="100px">
      <el-row :span="24">
        <el-col :span="6">
          <el-form-item label="养护名称">
            <el-input v-model="tblMain.param.name" size="small" placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="page-pane2">
        <el-button type="primary" size="small" @click="initTableData()">查询</el-button>
        <el-button size="small" @click="dataReset()">清空</el-button>
        <el-button size="small" type="info" @click="handleAdd()">新增</el-button>
        <!-- <el-button size="small" type="danger" @click="cancelEvent()">作废</el-button> -->
        <!-- <el-button size="small" type="danger" @click="deleteEvent()">删除</el-button> -->
      </div>
    </el-form>
    <div style="height: calc(100% - 160px);">
      <el-table ref="tblMain" style="margin-top: 10px;" :data="tblMain.items" @selection-change="handleSelectionChange"
        v-loading.body="tblMain.loading" height="100%" border stripe>
        <!-- <el-table-column type="selection" width="50" align="center"></el-table-column> -->
        <el-table-column type="index" width="80" label="序号" align="center"></el-table-column>
        <el-table-column label="操作" align="center" width="220px">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row, true)">查看</el-button>
            <el-divider v-if="scope.row.status == '0'||scope.row.status == '1'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.status == '0'||scope.row.status == '1'" type="text" @click="handleEdit(scope.row, false)">编辑</el-button>
            <el-divider v-if="scope.row.status == '1'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.status == '1'" type="text" @click="finishRow(scope.row)">完成</el-button>
            <el-divider v-if="scope.row.status == '0'" direction="vertical"></el-divider>
            <el-button v-if="scope.row.status == '0'" type="text" @click="deleteRow(scope.row)">删除</el-button>
            <!-- <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="cancelRow(scope.row)">作废</el-button> -->
          </template>
        </el-table-column>
        <el-table-column prop="status" width="100" label="状态" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status == '0'" type="primary">已提交</el-tag>
            <el-tag v-else-if="scope.row.status == '1'" type="warning">配件付款</el-tag>
            <el-tag v-else-if="scope.row.status == '2'" type="warning">养护支付</el-tag>
            <el-tag v-else-if="scope.row.status == '3'" type="success">已完成</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="养护名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="docNo" label="单据编号" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="customName" label="客户名称" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="numberplate" label="车牌号" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="totalPrice" label="费用合计" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="beginDate" label="养护开始时间" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column prop="endDate" label="养护结束时间" align="center" show-overflow-tooltip></el-table-column>
      </el-table>
      <el-pagination style="margin-top: 10px" :current-page.sync="tblMain.currentPage" :page-size="tblMain.pageSize"
        :total="tblMain.total" layout="total, sizes, prev, pager, next" @size-change="handleSizeChange"
        @current-change="handleCurrentChange"></el-pagination>
    </div>
    <maintain-dialog ref="modalForm" @ok="modalFormOk"></maintain-dialog>
  </div>
</template>
<script>
import MaintainDialog from './components/MaintainDialog'
import tool from '@/tool'
import {
  storeConserveMasterList,
  storeConserveMasterCancel,
  storeConserveMasterDel,
  storeConserveMasterComplete
} from '@/api/business'
export default ({
  name: 'MaintainList',
  components: {
    MaintainDialog
  },
  data () {
    return {
      tblMain: tool.genTableData({
        param: {
          name: '',
        },
        items: []
      }),
    }
  },
  mounted () {
    this.initTableData();
  },
  methods: {
    handleAdd () {
      this.$refs.modalForm.add();
    },
    handleEdit (val, flag) {
      this.$refs.modalForm.edit(val, flag);
    },
    handleSetFunction (val) {
      this.$refs.roleFunction.set(val);
    },
    modalFormOk () {
      this.initTableData();
    },
    async finishRow (val) {
      this.$confirm('确定完成该订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        params.push(val.id)
        await this.finishTblMain(params)
      }).catch(() => {
      })
    },
    finishTblMain (params) {
      let me = this
      me.tblMain.loading = true
      storeConserveMasterComplete({ ids: params }).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('设置成功')
          this.initTableData()
        } else {
          me.$message.error(data.data)
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    async cancelEvent () {
      if (this.tblMain.selection.length == 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('确定作废选中数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        this.tblMain.selection.forEach((element, index) => {
          params.push(element.id)
        })
        await this.cancelTblMain(params)
      }).catch(() => {
      })
    },
    async cancelRow (val) {
      this.$confirm('确定作废该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        params.push(val.id)
        await this.cancelTblMain(params)
      }).catch(() => {
      })
    },
    cancelTblMain (params) {
      let me = this
      me.tblMain.loading = true
      storeConserveMasterCancel({ ids: params }).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('设置成功')
          this.initTableData()
        } else {
          me.$message.error(data.data)
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    async deleteEvent () {
      if (this.tblMain.selection.length == 0) {
        this.$message.warning('请选择数据')
        return
      }
      this.$confirm('确定删除选中数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        this.tblMain.selection.forEach((element, index) => {
          params.push(element.id)
        })
        await this.deleteTblMain(params)
      }).catch(() => {
      })
    },
    // 单个删除
    async deleteRow (val) {
      this.$confirm('确定删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        let params = []
        params.push(val.id)
        await this.deleteTblMain(params)
      }).catch(() => {
      })
    },
    // 删除数据
    deleteTblMain (params) {
      let me = this
      me.tblMain.loading = true
      storeConserveMasterDel({ ids: params }).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.$message.success('删除成功')
          this.initTableData()
        } else {
          me.$message.error(data.data)
        }
      }).catch(() => {
        me.$message.error('请求失败')
        me.tblMain.loading = false
      })
    },
    dataReset () {
      this.tblMain.currentPage = 1
      this.tblMain.pageSize = 10
      this.tblMain.param.name = ''
      this.initTableData()
    },
    handleSelectionChange (val) {
      this.tblMain.selection = val;
    },
    handleSizeChange (val) {
      this.tblMain.pageSize = val
      this.initTableData()
    },
    handleCurrentChange (val) {
      this.tblMain.currentPage = val
      this.initTableData()
    },
    initTableData () {
      let me = this
      me.tblMain.selection = []
      let param = {
        name: me.tblMain.param.name,
        page: me.tblMain.currentPage,
        size: me.tblMain.pageSize
      }
      me.tblMain.loading = true
      storeConserveMasterList(param).then(res => {
        me.tblMain.loading = false
        let data = res.data
        if (data && data.code == 0) {
          me.tblMain.items = data.data.records
          me.tblMain.total = data.data.total
        }
      })
    },
  }
})
</script>
